package com.ncmmall.application.business.advertis;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.advertis.Advertis;
import com.ncmmall.domain.business.advertis.AdvertisRepository;
import com.ncmmall.domain.business.advertis.QAdvertis;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 广告管理
 *
 * @author likaikai
 * @date 2019-07-11 17:42:15
 */
@Service
@Transactional
public class AdvertisApplication {

    @Autowired
    private AdvertisRepository advertisRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 创建广告
     */
    public void create(Advertis advertis) {
        advertis.setStatus(1);
        advertis.setCreateTime(new Timestamp(System.currentTimeMillis()));
        advertisRepository.save(advertis);
    }

    /**
     * 编辑广告
     */
    public void modify(Advertis param) {

        Advertis advertis = queryChannelService.findOne(QAdvertis.advertis.id.eq(param.getId()), Advertis.class);
        advertis.setStatus(1);
        Requires.notNull(advertis, "广告不存在");

        advertis.setPosition(param.getPosition());
        advertis.setName(param.getName());
        advertis.setImage(param.getImage());
        advertis.setRank(param.getRank());
        advertis.setInnerLinkType(param.getInnerLinkType());
        advertis.setPlatformType(param.getPlatformType());

        advertis.setInnerLinkName(param.getInnerLinkName());
        advertis.setChannelId(param.getChannelId());
        advertis.setChannelLevel(param.getChannelLevel());

        //选择内链
        if (advertis.getInnerLinkType() == 3) {
            //内链搜索
            advertis.setInnerLinkKeyword(param.getInnerLinkKeyword());
        } else {
            //内链非搜索，设置id
            advertis.setInnerLinkId(param.getInnerLinkId());
        }

        advertisRepository.save(advertis);
    }


    /**
     * 上架
     */
    public void enabled(long id) {

        Advertis advertis = queryChannelService.findOne(QAdvertis.advertis.id.eq(id), Advertis.class);
        Requires.notNull(advertis, "广告不存在");
        List<Integer> indexList = Lists.newArrayList(2,4,5,6,7,8,9);
        if (advertis.getPlatformType() == 0) {
            //首页通栏广告 商品支付广告 课程支付广告最多存在一条
            if (indexList.contains(advertis.getPosition())) {
                int count = queryChannelService.selectCount("select count(*) from mall_advertis where position = :position and platformType = 0 and status = 2", ImmutableMap.of("position", advertis.getPosition()));
                Requires.isTrue(count < 1, "当前种类广告数量超出限制");
            } else if (advertis.getPosition() == 3) {
                //首页小广告最多存在4条
                int count = queryChannelService.selectCount("select count(*) from mall_advertis where position = :position and platformType = 0 and status = 2", ImmutableMap.of("position", advertis.getPosition()));
                Requires.isTrue(count < 4, "当前种类广告数量超出限制");
            }
        }

        if (advertis.getPlatformType() == 1) {
            //首页通栏广告 商品支付广告 课程支付广告最多存在一条
            if (indexList.contains(advertis.getPosition())) {
                int supplierCount = queryChannelService.selectCount("select count(*) from mall_advertis where position = :position and platformType = 1 and status = 2", ImmutableMap.of("position", advertis.getPosition()));
                Requires.isTrue(supplierCount < 1, "当前种类广告数量超出限制");
            } else if (advertis.getPosition() == 3) {
                //首页小广告最多存在4条
                int supplierCount = queryChannelService.selectCount("select count(*) from mall_advertis where position = :position and platformType = 1 and status = 2", ImmutableMap.of("position", advertis.getPosition()));
                Requires.isTrue(supplierCount < 4, "当前种类广告数量超出限制");
            }
        }

        advertis.setStatus(2);
        advertisRepository.save(advertis);

    }


    /**
     * 下架
     */
    public void disable(long id) {
        Advertis advertis = queryChannelService.findOne(QAdvertis.advertis.id.eq(id), Advertis.class);
        Requires.notNull(advertis, "广告不存在");
        advertis.setStatus(1);
        advertisRepository.save(advertis);
    }

    /**
     * 删除
     */
    public void remove(long id) {
        advertisRepository.delete(id);
    }

    /**
     * 批量下架
     */
    public void disables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Long::parseLong).collect(Collectors.toList());
        for (Long id : ids) {
            disable(id);
        }
    }

    /**
     * 批量上架
     */
    public void enables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Long::parseLong).collect(Collectors.toList());
        for (Long id : ids) {
            enabled(id);
        }
    }
}
